let animation = wx.createAnimation({
  duration: 4000,//转盘转动时间
  timingFunction: 'ease-out'//动画效果
})
const app = getApp();
Page({
  data: {
    openid: '',
   
    CUSTOMER_TEL:'',
    phone: '',//手机号
    rotate: 0, //度数
    turning: false, //是否点击,
    zhunsj: '',//另一个页面带来的数据
    prize: '',//中奖概率
    count: '',//剩余次数
    prizeList: [
      2100,//一等奖 ...
      2400,//二等奖  ...
      2330,//三等奖...
      2610,//参与奖 ...
      2200,//谢谢惠顾 ...
    ],
    //向1006传递的数据
    USER_PRIZE_ID: '',
    txtlist: '',//翻滚的字幕
    flag: true,//设置节流
    modalHidden: false,//新的图片模态框
    ajaj: '',//获取中奖奖项
 
    jiang_x: '',//奖品等级
    EMPLOYEE_ID: '',
    wang_d: '',//地点
    city: [],//市
    district: [],//区
    district: [],//区
    business: '',//网点
    shi_k: true,
    qu_k: true,
    wang_k: true,
    tishi: true,

    L2code: '',
    L3code: '',

    ad_img: '',//中奖图片
    // earth: true,//地区弹框
    // Getphone: false,//判断获取手机号

    session_key:'',
  },

  //判断是否授权
  // authorization() {
  //   if (app.globalData.grant) {
  //     this.setData({
  //       Getphone: true
  //     });
  //   } else {
  //     wx.showModal({
  //       title: '温馨提示',
  //       content: '请先绑定会员信息',
  //       success: function (res) {
  //         if (res.confirm) {//这里是点击了确定以后
  //           wx.navigateTo({
  //             url: '../../pages/member/member',
  //           })
  //         } else {//这里是点击了取消以后
  //           console.log('用户点击取消')
  //         }
  //       }
  //     })
  //   }

  // },


//   //跳转授权
// authorization(){
//   wx.navigateTo({
//     url: '../../pages/member/member',
//   })
// },


  showPopup() {
    this.setData({
      modalHidden: !this.data.modalHidden,
      earth: false,
    });
  },

  //自定义模态框的取消
  modalCancel: function () {
    let animation = wx.createAnimation({
      duration: 100,
      timingFunction: 'ease-out'
    });
    animation.rotate(0).step();
    this.setData({
      modalHidden: true,
      erdeng: true,
      sandeng: true,
      sideng: true,
      xiexie: true,
      rotate: animation.export(),
    })
    console.log(222);
  },

  //自定义模态框的确定
  queding: function () {
    let animation = wx.createAnimation({
      duration: 100,
      timingFunction: 'ease-out'
    });
    animation.rotate(0).step();
    this.setData({
      modalHidden: true,
      erdeng: true,
      sandeng: true,
      sideng: true,
      xiexie: true,
      rotate: animation.export(),
    })
    console.log(111);
  },


  start: function () {
    let that = this
    // 节流
    if (that.data.flag) {
     
      // 查询是否抽奖
      wx.request({
        url: app.globalData.interface+'/newVersion/API_2023_checkPersonalRaffle',
        method: 'POST',
        data: {
          OPEN_ID: that.data.openid,
          USER_ASSETS_IDCARD: that.data.phone,
          session_key:this.data.session_key,
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          
          console.log(res.data);

          
          if (res.data.result != 'success') {
            that.setData({
              tishi: false,
              wang_d: res.data.employee,
              jiang_x: res.data.guideUserPrize.PRIZE_ID
            })
          } else {
             that.data.flag = false
            wx.request({
              url: app.globalData.interface+'/newVersion/API_2021_personalRaffle',
              method: 'POST',
              data: {
                OPEN_ID: that.data.openid,
                USER_ASSETS_IDCARD: that.data.phone,
                session_key:this.data.session_key,
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: res => {
                console.log(res.data.prizeMessage.STOCK_ID);
                that.setData({
                  ad_img: res.data.prizeMessage.STOCK_ID,
                  prize: res.data.prize,
                })
                setTimeout(() => {
                  that.setData({
                    ajaj: res.data.data,
                  })
                }, 6000)
                const index_ = that.data.prize;
                that.rotate(index_)
              }
            })
            setTimeout(() => {
              that.data.flag = true
            }, 5500)

          }
        }
      })
    }
  },

  // 转盘代码
  rotate: function (index_) {
    // rotate: function () {
    let _this = this;
    // let cat = 60;
    if (!this.data.turning) {
      let rdm = 0; //度数
      rdm = Math.floor(Math.random() * 3600); //最大10圈
      // rdm = 2100; //最大10圈
      //用索引的方法对应角度
      rdm = this.data.prizeList[index_ - 1];//直接确定转的度数 
      // rdm =2200;//直接确定转的度数 
      animation.rotate(rdm).step();
      this.setData({
        rotate: animation.export(),
        turning: true
      });
      setTimeout(() => {
        this.setData({
          turning: false
        });
        let num = rdm % 360; //转了多少度
        //console.log(num);
        function showModal(str) {
          wx.showModal({
            title: '恭喜抽中',
            content: str,
            success: function () { //点击确定后还原到0度
              let animation = wx.createAnimation({
                duration: 100,
                timingFunction: 'ease-out'
              });
              animation.rotate(0).step();
              _this.setData({
                rotate: animation.export(),
                turning: false
              });
            },
            fail: function (res) {
              console.log(res)
            }
          })
        }

        if (rdm == 2200) {
          // showModal('谢谢惠顾');
          this.setData({
            modalHidden: true
          })
        } else if (rdm == 2100) {
          // showModal('一等奖');
          this.setData({
            modalHidden: true
          })
        } else if (rdm == 2330) {
          // showModal('三等奖');
          this.setData({
            modalHidden: true
          })
        } else if (rdm == 2610) {
          // showModal('参与奖');
          this.setData({
            modalHidden: true
          })
        } else if (rdm == 2400) {
          // showModal('二等奖');
          this.setData({
            modalHidden: true
          })
        }
      }, 4500)
    }
  },

  // 地区数据
  region() {
    wx.request({
      url: app.globalData.interface+'/newVersion/API_2029_outletPush',
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res.data.data);
        this.setData({
          city: res.data.data,
        })

      }
    })
  },


  but_shi() {
    this.setData({
      shi_k: false
    })
  },

  but_xian() {
    if (this.data.L2code == '') {
      wx.showToast({
        title: '请先选择市',
        icon: 'error',
        duration: 2000
      })
    } else {
      this.setData({
        qu_k: false
      })
    }
  },

  but_w() {

    if (this.data.L2code == '') {
      wx.showToast({
        title: '请先选择区',
        icon: 'error',
        duration: 2000
      })
    } else {
      this.setData({
        wang_k: false
      })
    }

  },


  //市
  buttn_c(e) {
    console.log(e);
    console.log(e.currentTarget.dataset.item.district);
    console.log(e.currentTarget.dataset.item.L2code);
    this.setData({
      district: e.currentTarget.dataset.item.district,
      L2code: e.currentTarget.dataset.item.L2code,
      L2name: e.currentTarget.dataset.item.L2name,
      shi_k: true

    })

  },

  buttn_x(e) {
    console.log(e);


    this.setData({
      L3code: e.currentTarget.dataset.item.L3code,
      L3name: e.currentTarget.dataset.item.L3name,
      qu_k: true
    })
    this.Outlets()

  },


  buttn_w(e) {
    console.log(e);
    this.setData({
      L4name: e.currentTarget.dataset.item.L4name,
      EMPLOYEE_ID: e.currentTarget.dataset.item.EMPLOYEE_ID,
      wang_k: true
    })
  },

  //网点
  Outlets() {
    wx.request({
      url: app.globalData.interface+'/newVersion/API_2027_outletList',
      method: 'POST',
      data: {
        L2code: this.data.L2code,
        L3code: this.data.L3code
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: res => {
        console.log(res);
        console.log(res.data.data);
        this.setData({
          business: res.data.data
        })

      }
    })
  },


  qu_ding() {
    let that = this
    if (that.data.EMPLOYEE_ID == '') {
      wx.showToast({
        title: '请先选择地址',
        icon: 'error',
        duration: 2000
      })
    } else {
      wx.request({
        url: app.globalData.interface+'/newVersion/API_2025_selectOutlet',
        method: 'POST',
        data: {
          OPEN_ID: that.data.openid,
          USER_ASSETS_IDCARD: that.data.phone,
          EMPLOYEE_ID: that.data.EMPLOYEE_ID
        },
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        success: res => {
          console.log(res);
          console.log(res.data.result);
          if (res.data.result == 'success') {

            wx.showModal({
              title: '温馨提示',
              content: '已为您备好礼品，尽快来网点领取吧！',
              success: function (res) {
                if (res.confirm) {//这里是点击了确定以后
                  console.log('用户点击确定')
                } else {//这里是点击了取消以后
                  console.log('用户点击取消')
                }
              }
            })
            that.setData({
              earth: true,
            });
          }
        }
      })

    }

  },

  //关闭显示
  guan_b() {
    this.setData({
      tishi: true
    })
  },

    // //跳转授权
    // authorization() {
    //   var that = this
    //   wx.request({
    //     url: app.globalData.interface+'/newVersion/API_2007_getPhoneByOpenId',
    //     method: 'POST',
    //     //向后端发送的数据
    //     data: {
    //       OPEN_ID: this.data.openid  //将code发送到后台服务器。
    //     },
    //     header: {
    //       "Content-Type": "application/x-www-form-urlencoded"  //POST方式是这个
    //     },
    //     success: res => {
    //       console.log(res);
    //       console.log(res.data.data.CUSTOMER_TEL);
    //       if (res.data.result != 'success') {
    //         wx.showModal({
    //           title: '温馨提示',
    //           content: '请先绑定会员信息',
    //           success: function (res) {
    //             if (res.confirm) {//这里是点击了确定以后
    //               wx.navigateTo({
    //                 url: '../../pages/member/member',
    //               })
    //             } else {//这里是点击了取消以后
    //               console.log('用户点击取消')
    //             }
    //           }
    //         })
    //         that.setData({
    //           Getphone: false,
    //         })
    //       } else {
    //         var phone = res.data.data.CUSTOMER_TEL;
    //         app.globalData.phone = phone;
    //         that.setData({
    //           CUSTOMER_TEL: res.data.data.CUSTOMER_TEL,
    //           LEVEL_IMG: res.data.data.LEVEL_IMG,
    //           // CUSTOMER_TEL: res.data.data.CUSTOMER_TEL,
    //           Getphone: true,
    //         })
    //       }
    //     }
    //   })
    // },









  onLoad: function (e) {
    //从上个页面带来的数据
    console.log(e.data)
    this.setData({
      phone:app.globalData.phone,
      session_key: app.globalData.session_key,
    })

  },


  onShow() {

    // this.authorization() 


    this.region()
    // this.setData({
    //   openid: app.globalData.openid,
    // });
    // console.log(this.data.openid);

    var that = this
    wx.login({
      success: function (res) {
        console.log(res);
        if (res.code) {  //wx.login获取code。
          console.log(res.code);
          //发起网络请求
          wx.request({
            url: app.globalData.interface+'/post/API_1062_appGetOpenid',
            method: 'GET',
            //向后端发送的数据
            data: {
              code: res.code  //将code发送到后台服务器。
            },
            header: {
              "Content-Type": "application/x-www-form-urlencoded" //POST方式是这个
            },
            success: res => {
              console.log(res);
              if (res.data.result != 'success') {
                // wx.showToast({
                //   title: '请获取手机号',
                //   duration: 1000
                // })
                console.log(1);
              } else {

                var openid = res.data.openid;
                app.globalData.openid = openid;

                console.log(res.data.openid);
                that.setData({
                  openid: res.data.openid
                })
                // that.scanCod()
              }
            }
          })
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    });

  },




})


wx.showShareMenu({
  withShareTicket: true,
  menus: ['shareAppMessage', 'shareTimeline']
})